package com.example.lib_common.view.view

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.databinding.ViewDataBinding
import com.example.lib_base.view.BaseFragment
import com.example.lib_common.view.event.CustomEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseMvvmFragment<VDB:ViewDataBinding>:BaseFragment<VDB>() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //获取数据
        loadData()
        //订阅数据
        observeDataChanged()
        //注册事件
        EventBus.getDefault().register(this)

    }

    //接收事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onReceiveEvent(event: CustomEvent?){
        //校验是否为当前页面接收事件
        if (event != null && event.target == this.javaClass.simpleName){
            handEvent(event)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }


    //可以根据业务需要重写方法
    protected open fun loadData(){ }

    //可以根据业务需要重写方法
    protected open fun observeDataChanged(){ }

    //交给子类处理事件
    protected open fun handEvent(event: CustomEvent){

    }
}